package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements IOnTimeChangeListenner {
    private Button starService;
    private TextView txtTime;
private MyReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MainActivity.this","thread id="+Thread.currentThread().getId());
        receiver=new MyReceiver(MainActivity.this);
        IntentFilter filter=new IntentFilter();
        filter.addAction("TIMR_CHAGE");
       registerReceiver(receiver,filter);
        starService=findViewById(R.id.star_service);
        starService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        txtTime=findViewById(R.id.txt_time);

    }
    //定义内部类
    /*class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String timeStr=intent.getStringExtra("time");
            txtTime.setText(timeStr);
        }
    }
*/
    /*定义外部类 IOnTimeChangeListenner MyReceiver*/
    public void show(String str){
        txtTime.setText(str);
    }
}
